home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The PC-SIG Library 10
/
The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso
/
PC_SIGCD
/
05
/
6
/
DISK0564.ZIP
/
SOURCE.ARC
/
B.ARC
/
FIXPATH.C
< prev
next >
Wrap
C/C++ Source or Header
|
1986-03-29
|
4KB
|
109 lines
#include "fixpath.h"
#define NULL (void *)0
extern char *index();
extern unsigned int doscall();
static struct regstruc { /* used by function 'doscall' */
unsigned int ax,bx,cx,dx,si,di;
} inregs,outregs;
int fixpath(ip,sp,lip)
/* THIS FUNCTION PARSES A (POSSIBLY AMBIGUOUS) DOS PATH NAME */
/* INTO A SEARCH PATH AND A LEAD-IN PATH. THE SEARCH PATH CAN */
/* BE USED AS AN ARGUMENT TO DOS FUNCTIONS 4E AND 4F. THE */
/* LEAD-IN PATH IS INTENDED TO BE PREFIXED TO THE SIMPLE FILENAME */
/* RETURNED BY THESE FUNCTIONS, IN ORDER TO PRODUCE A COMPLETE */
/* PATH NAME USABLE BY OPEN, RENAME, ETC. */
/* */
/* ON RETURN, THE FUNCTION VALUE IS SET AS FOLLOWS: */
/* 0 IF PATH IS AN UNAMBIGUOUS FILE NAME */
/* 1 IF PATH IS AN AMBIGUOUS FILE NAME (E.G. "FOO?.*") */
/* 2 IF PATH IS A DIRECTORY SPEC (E.G.: A:\FOO\BAR\") */
/* 3 IF PATH IS A DRIVE SPEC (E.G. "A:") OR NULL STRING */
/* 4 IF PATH IS A DIRECTORY */
/* 5 IF PATH TYPE UNKNOWN OR DOES NOT EXIST */
/* */
/* WRITTEN FOR AZTEC C86, V. 3.20e */
/* BY JON DART, 1866 DIAMOND ST., SAN DIEGO, CA 92109 */
/* */
/* MODELLED AFTER THE C FUNCTION fixpath() IN DR. DOBB'S JOURNAL, */
/* OCT. 1985, PP. 16-18. */
char *ip; /* input path */
register char *sp, /* search path */
*lip; /* lead-in or prefix path */
{
char *cp, lastch;
int n,type,len;
len = strlen(ip);
if ( (len==0) /* null string */
|| (strcmp(ip+1,":")==0)) { /* d: only */
type = TYPE_DRV;
}
else if ((lastch = ip[len-1])=='\\') { /* ends in backslash */
type = TYPE_DSP;
}
else if (lastch == '.') {
if (strcmp(ip,"..")==0) {
type = TYPE_UNK; /* we know ".." is a directory, but we
will make sure such a directory exists,
before returning the type */
}
else { /* single dot at end of ip, assume it's a directory spec */
strcpy(lip,ip);
strcpy(sp,ip);
lip[len-1]='\000'; /* remove dot from end of lip */
sp[len-1] ='\000'; /* remove dot from end of sp */
strcat(sp,"*.*");
return(TYPE_DSP);
}
}
if ((type==TYPE_DSP) || (type==TYPE_DRV)) {
strcpy(lip,ip);
strcpy(sp,ip);
strcat(sp,"*.*");
return(type);
}
else { /* use dos call 43 (get attribute) to find type of path */
inregs.ax = 0;
inregs.dx = (unsigned int) ip;
inregs.cx = 0;
if ( doscall(0x43,&inregs,&outregs) ) { /* failed to parse path */
if (outregs.ax==0x0003) { /* might be valid (but ambiguous) path */
if ((index(ip,'*')!=NULL)
|| (index(ip,'?')!=NULL)) { /* is ambiguous path name */
type = TYPE_AFN;
}
else /* dos fn. 43 choked, something wrong */
type = TYPE_UNK;
}
else /* weird error code from dos fn. 43 */
type = TYPE_UNK;
}
else { /* something corresponding to path exists on disk */
if (outregs.cx & 0x0010) /* path is directory */
type = TYPE_DIR;
else /* path is unambiguous file name */
type = TYPE_UFN;
}
}
if (type==TYPE_DIR) { /* directory */
strcpy(sp,ip);
strcpy(lip,ip);
strcat(sp,"\\*.*");
strcat(lip,"\\");
}
else { /* AFN, UFN or UNK */
strcpy(sp,ip);
cp = sp+len-1;
for(;cp>sp;--cp)
if (('\\'==*cp) || (':'==*cp)) break;
if (cp>sp) ++cp; /* retain colon/slash */
*cp='\000';
strcpy(lip,sp);
strcpy(sp,ip);
}
return(type);
}